package com.engine.demo.cmd;

import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.constant.*;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.demo.dao.DemoDao;
import com.engine.systeminfo.constant.em.EmConstant;
import com.engine.systeminfo.util.DataUtils;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @version 1.0
 * @author AndyZhang
 * TODO 
 */
public class DoChangeDemoStatus extends AbstractCommonCommand<Map<String,Object>> {

	private final static Log log = LogFactory.getLog(DoChangeDemoStatus.class);
	
    private BizLogContext bizLogContext = new BizLogContext();
    public DoChangeDemoStatus(Map<String,Object> params,User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public BizLogContext getLogContext() {
        String ruleid = Util.null2String(params.get("id"));
        String _flagInfo = "更改状态";
        bizLogContext.setDateObject(new Date());
        bizLogContext.setUserid(user.getUID());
        bizLogContext.setUsertype(Util.getIntValue(user.getLogintype()));
        bizLogContext.setTargetId(ruleid);
        bizLogContext.setLogType(BizLogType.SYSTEM_ENGINE);
        bizLogContext.setLogSmallType(BizLogSmallType4SysEngine.SYSTEM_ENGINE_EM);
        bizLogContext.setOperateType(BizLogOperateType.UPDATE);
        bizLogContext.setParams(params);
        bizLogContext.setClientIp(Util.null2String(params.get(ParamConstant.PARAM_IP)));
        bizLogContext.setDesc(String.format(user.getLastname() + "对：规则管理做了"+_flagInfo+"操作， "+_flagInfo+"的mapid是：{"+ruleid+"}  "));
        return bizLogContext;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
    	WeaResultMsg result=new WeaResultMsg(true);

        String id = Util.null2String(params.get("id"));
        RecordSetTrans recordSet = new RecordSetTrans();
        String sql = DemoDao.getOne();
        try {
            recordSet.setAutoCommit(false);
            recordSet.executeQuery(sql,id);
            if(recordSet.next()) {
                String status = recordSet.getString("status");//当前状态
                boolean disabled = false;
                if(EmConstant.DISABLE_EM.equalsIgnoreCase(status)){//如果为“禁用”状态，则关闭所有已开启
                    sql="update ECOLOGY_DEMO set status=? where status=?";
                    recordSet.executeUpdate(sql,EmConstant.DISABLE_EM,EmConstant.ABLE_EM);
                    disabled=true;
                }
                sql="update ECOLOGY_DEMO set status=? where id=?";
                recordSet.executeUpdate(sql,disabled?EmConstant.ABLE_EM:EmConstant.DISABLE_EM,id);//替换已有状态
                recordSet.commit();
                result.success();
                return result.getResultMapAll();
            }
        }catch (Exception e){
            recordSet.rollback();
            e.printStackTrace();
            log.error(e);
            result.fail(e.getMessage());
            
        }
        return result.getResultMapAll();
    }
}
